package cz.acies.http.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cz.acies.data.UserContext;

public class AuthenticationFilter implements Filter
{
	private FilterConfig config;
	
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse)resp;
		HttpServletRequest  request  = (HttpServletRequest)req;
		
		UserContext uCtx = (UserContext)request.getSession().getAttribute("uCtx");		
		if (uCtx != null) {
			chain.doFilter(req, resp);
		} else {
			response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied");
			//response.setStatus(HttpServletResponse.SC_FORBIDDEN);
			//response.sendRedirect("/genea/index.jsp");
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}

	@Override
	public void destroy() {
		this.config = null;
	}
}
